package com.ljd.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.ljd.music.pojo.Consumer;
import com.ljd.music.service.ConsumerService;
import com.ljd.music.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {


    @Autowired
    private ConsumerService consumerService;


    @PostMapping("add")
    public Object addConsumer (Consumer consumer) {
        JSONObject jsonObject = new JSONObject();
        boolean insert = consumerService.insert(consumer);

        if (insert) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"增加成功");
        }else {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"增加失败");
        }

        return jsonObject;
    }


    @GetMapping("allConsumer")
    public Object allConsumer () {
        return consumerService.allConsumer();
    }

    @PostMapping("update")
    public Object update (Consumer consumer) {
        JSONObject jsonObject = new JSONObject();

        System.out.println(consumer);
        boolean update = consumerService.update(consumer);


        if(update) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");
        }else {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"修改失败");
        }

        return jsonObject;
    }

    @GetMapping("delete")
    public Object delete (Integer id) {
        JSONObject jsonObject = new JSONObject();

        boolean delete = consumerService.delete(id);



        if(delete) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除成功");
        }else {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除失败");
        }

        return jsonObject;

    }

}
